/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.xml;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.ParseException;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import nhadat.dao.TindangDao;
import nhadat.entity.TblTinDang;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import nhadat.dao.KhuVucDAO;
import nhadat.dao.LoaiGiayToDAO;
import nhadat.dao.LoaiNhaDAO;
import nhadat.dao.LoaiTinDAO;
import nhadat.entity.TblKhuVuc;
import nhadat.entity.TblLoaiGiayTo;
import nhadat.entity.TblLoaiNha;
import nhadat.entity.TblLoaiTin;
import nhadat.entity.TblNguoiDung;
import nhadat.tindang.DstindangType;
import nhadat.tindang.TindangType;
import nhadat.tindangfull.TindangfullType;
import org.w3c.dom.ProcessingInstruction;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;

/**
 *
 * @author DUNGND60190
 */
public class XMLmanipulate {

    public XMLmanipulate() {
    }

//    public static void main(String[] args) {
//        XMLmanipulate test = new XMLmanipulate();
//
//        KhuVucDAO khuvucdao = new KhuVucDAO();
//        TblKhuVuc[] dskhuvuc = khuvucdao.getAllKhuVuc();
//        String outputXML = "web/xml/TblKhuVuc.xml";
//        test.createXMLfromTblKhuVuc(dskhuvuc, outputXML);
//
//        LoaiGiayToDAO gtDao = new LoaiGiayToDAO();
//        TblLoaiGiayTo[] dsgiayto = gtDao.getAllLoaiGiayTo();
//        outputXML = "web/xml/TblLoaiGiayTo.xml";
//        test.createXMLfromTblLoaiGiayTo(dsgiayto, outputXML);
//
//        LoaiNhaDAO nhaDao = new LoaiNhaDAO();
//        TblLoaiNha[] dsloainha = nhaDao.getAllLoaiNha();
//        outputXML = "web/xml/TblLoaiNha.xml";
//        test.createXMLfromTblLoaiNha(dsloainha, outputXML);
//
//        LoaiTinDAO tinDao = new LoaiTinDAO();
//        TblLoaiTin[] dsLoaitin = tinDao.getAllLoaiTin();
//        outputXML = "web/xml/TblLoaiTin.xml";
//        test.createXMLfromTblLoaiTin(dsLoaitin, outputXML);
//    }
    public void createPackageFromXmlXsd(String inputXSD, String packageName) {
        try {
            String output = "src/java/";
            SchemaCompiler src = XJC.createSchemaCompiler();
            src.setErrorListener(new ErrorListener() {

                public void error(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void fatalError(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void warning(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void info(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            });
            src.forcePackageName(packageName);
            File schema = new File(inputXSD);
            InputSource input = new InputSource(schema.toURI().toString());
            src.parseSchema(input);
            S2JJAXBModel model = src.bind();
            JCodeModel code = model.generateCode(null, null);
            code.build(new File(output));
            System.out.println("FINISHED");
        } catch (IOException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * createTindangTypeFromXML
     * Unmarshall: OBject TindangType to XML
     * @param inputXML
     * @return
     */
    public List<TindangType> createTindangTypeFromXML(String inputXML) {
        try {
            JAXBContext jc = JAXBContext.newInstance("nhadat.tindang");
            Unmarshaller u = jc.createUnmarshaller();
            File f = new File(inputXML);
            JAXBElement jax = (JAXBElement) u.unmarshal(f);
            DstindangType tindang = (DstindangType) jax.getValue();
            List<TindangType> resultTindang = tindang.getTindang();
            return resultTindang;
        } catch (JAXBException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * createXMLfromTindangType
     * Using DOM + TRAX to write Object to XML
     * @param tindangs
     * @param outputXML
     */
    public void createXMLfromTindangType(TindangType[] tindangs, String outputXML) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
//            doc.setXmlStandalone(false);


            Element dstindang = doc.createElement("dstindang");
            doc.appendChild(dstindang);


            // <editor-fold defaultstate="collapsed" desc="TindangType">
            // Element dstindang
            for (int i = 0; i < tindangs.length; i++) {
                TindangType TinDang = tindangs[i];
                Element tindang = doc.createElement("tindang");
                tindang.setAttribute("status", TinDang.getStatus());
                dstindang.appendChild(tindang);

                Element matindang = doc.createElement("matindang");
                matindang.setTextContent(TinDang.getMatindang());
                tindang.appendChild(matindang);

                Element tenloainha = doc.createElement("tenloainha");
                tenloainha.setTextContent(TinDang.getTenloainha());
                tindang.appendChild(tenloainha);

                Element tenloaigiayto = doc.createElement("tenloaigiayto");
                tenloaigiayto.setTextContent(TinDang.getTenloaigiayto());
                tindang.appendChild(tenloaigiayto);

                //Get column dientichkhuonvien: 40 - dvddientichkhuongvien : m2
                Element dientichkhuonvien = doc.createElement("dientichkhuonvien");
                tindang.appendChild(dientichkhuonvien);

                Element dvddientichkhuonvien = doc.createElement("dvddientichkhuonvien");
                dvddientichkhuonvien.setTextContent(TinDang.getDientichkhuonvien().getDvddientichkhuonvien());
                dientichkhuonvien.appendChild(dvddientichkhuonvien);

                Element solieu = doc.createElement("solieu");
                solieu.setTextContent(Double.toString(TinDang.getDientichkhuonvien().getSolieukhuonvien()));
                dientichkhuonvien.appendChild(solieu);
                // END GET-----------------------------------

                //Get column dientichxaydung: 40 - dientichxaydung : m2
                Element dientichxaydung = doc.createElement("dientichxaydung");
                tindang.appendChild(dientichxaydung);

                Element dvddientichxaydung = doc.createElement("dvddientichxaydung");
                dvddientichxaydung.setTextContent(TinDang.getDientichxaydung().getDvddientichxaydung());
                dientichxaydung.appendChild(dvddientichxaydung);

                Element solieudientichxaydung = doc.createElement("solieu");
                solieudientichxaydung.setTextContent(Double.toString(TinDang.getDientichxaydung().getSolieudientich()));
                dientichxaydung.appendChild(solieudientichxaydung);
                // END GET-----------------------------------

                //Get khu vuc - quan huyen - tinh thanh
                Element khuvuc = doc.createElement("khuvuc");
                tindang.appendChild(khuvuc);

                Element quanhuyen = doc.createElement("quanhuyen");
                quanhuyen.setTextContent(TinDang.getKhuvuc().getQuanhuyen());
                khuvuc.appendChild(quanhuyen);

                Element tinhthanh = doc.createElement("tinhthanh");
                tinhthanh.setTextContent(TinDang.getKhuvuc().getTinhthanh());
                khuvuc.appendChild(tinhthanh);
                //END GET------------------------------------


                //Get column giatien: 300 - dvdgiatien : VND
                Element giatien = doc.createElement("giatien");
                tindang.appendChild(giatien);

                Element dvdgiatien = doc.createElement("dvdgiatien");
                dvdgiatien.setTextContent(TinDang.getGiatien().getDvdgiatien());
                giatien.appendChild(dvdgiatien);

                Element solieugiatien = doc.createElement("solieu");
                solieugiatien.setTextContent(Double.toString(TinDang.getGiatien().getSolieugiatien()));
                giatien.appendChild(solieugiatien);
                // END GET-----------------------------------

                Element hinhanh = doc.createElement("hinhanh");
                hinhanh.setTextContent(TinDang.getHinhanh());
                tindang.appendChild(hinhanh);

                Element tieudetin = doc.createElement("tieudetin");
                tieudetin.setTextContent(TinDang.getTieudetin());
                tindang.appendChild(tieudetin);

                Element tenloaitin = doc.createElement("tenloaitin");
                tenloaitin.setTextContent(TinDang.getTenloaitin());
                tindang.appendChild(tenloaitin);

                Element nguoidangtin = doc.createElement("nguoidangtin");
                nguoidangtin.setTextContent(TinDang.getNguoidangtin());
                tindang.appendChild(nguoidangtin);

                Element ngaydangtin = doc.createElement("ngaydangtin");
                ngaydangtin.setTextContent(TinDang.getNgaydangtin());
                tindang.appendChild(ngaydangtin);

                Element lancuoicapnhat = doc.createElement("lancuoicapnhat");
                lancuoicapnhat.setTextContent(TinDang.getLancuoicapnhat());
                tindang.appendChild(lancuoicapnhat);

                Element soluotxem = doc.createElement("soluotxem");
                soluotxem.setTextContent(TinDang.getSoluotxem().toString());
                tindang.appendChild(soluotxem);
            }
            //</editor-fold>

            TransformerFactory tsf = TransformerFactory.newInstance();
            Transformer ts = tsf.newTransformer();

            ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            ts.setOutputProperty(OutputKeys.INDENT, "no");

            StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
            DOMSource dom = new DOMSource(doc);
            ts.transform(dom, rs);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void createXMLfromTindangFullType(TindangfullType[] tindangs, String outputXML) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
//            doc.setXmlStandalone(false);


            Element dstindang = doc.createElement("dstindang");
            doc.appendChild(dstindang);


            // <editor-fold defaultstate="collapsed" desc="TindangType">
            // Element dstindang
            for (int i = 0; i < tindangs.length; i++) {
                TindangfullType TinDang = tindangs[i];
                Element tindang = doc.createElement("tindang");
                tindang.setAttribute("status", TinDang.getStatus());
                dstindang.appendChild(tindang);

                Element matindang = doc.createElement("matindang");
                matindang.setTextContent(TinDang.getMatindang());
                tindang.appendChild(matindang);

                Element tenloainha = doc.createElement("tenloainha");
                tenloainha.setTextContent(TinDang.getTenloainha());
                tindang.appendChild(tenloainha);

                Element tenloaigiayto = doc.createElement("tenloaigiayto");
                tenloaigiayto.setTextContent(TinDang.getTenloaigiayto());
                tindang.appendChild(tenloaigiayto);

                Element cautruc = doc.createElement("cautruc");
                cautruc.setTextContent(TinDang.getCautruc());
                tindang.appendChild(cautruc);

                Element thongtinchitiet = doc.createElement("thongtinchitiet");
                thongtinchitiet.setTextContent(TinDang.getThongtinchitiet());
                tindang.appendChild(thongtinchitiet);

                //Get column dientichkhuonvien: 40 - dvddientichkhuongvien : m2
                Element dientichkhuonvien = doc.createElement("dientichkhuonvien");
                tindang.appendChild(dientichkhuonvien);

                Element dvddientichkhuonvien = doc.createElement("dvddientichkhuonvien");
                dvddientichkhuonvien.setTextContent(TinDang.getDientichkhuonvien().getDvddientichkhuonvien());
                dientichkhuonvien.appendChild(dvddientichkhuonvien);

                Element solieu = doc.createElement("solieu");
                solieu.setTextContent(Double.toString(TinDang.getDientichkhuonvien().getSolieu()));
                dientichkhuonvien.appendChild(solieu);

                //Get column chieu ngang khuon vien
                Element chieungangkhuonvien = doc.createElement("chieungangkhuonvien");
                tindang.appendChild(chieungangkhuonvien);

                Element dvdchieungangkhuonvien = doc.createElement("dvdchieungangkhuonvien");
                dvdchieungangkhuonvien.setTextContent(TinDang.getChieungangkhuonvien().getDvdchieungangkhuonvien());
                chieungangkhuonvien.appendChild(dvdchieungangkhuonvien);

                Element solieuchieungangkhuonvien = doc.createElement("solieu");
                solieuchieungangkhuonvien.setTextContent(Double.toString(TinDang.getChieungangkhuonvien().getSolieu()));
                chieungangkhuonvien.appendChild(solieuchieungangkhuonvien);

                //Get column chieu dai khuon vien
                Element chieudaikhuonvien = doc.createElement("chieudaikhuonvien");
                tindang.appendChild(chieudaikhuonvien);

                Element dvdchieudaikhuonvien = doc.createElement("dvdchieudaikhuonvien");
                dvdchieudaikhuonvien.setTextContent(TinDang.getChieudaikhuonvien().getDvdchieudaikhuonvien());
                chieudaikhuonvien.appendChild(dvdchieudaikhuonvien);

                Element solieuchieudaikhuonvien = doc.createElement("solieu");
                solieuchieudaikhuonvien.setTextContent(Double.toString(TinDang.getChieudaikhuonvien().getSolieu()));
                chieudaikhuonvien.appendChild(solieuchieudaikhuonvien);
                // END GET-----------------------------------

                //Get column dientichxaydung: 40 - dientichxaydung : m2
                Element dientichxaydung = doc.createElement("dientichxaydung");
                tindang.appendChild(dientichxaydung);

                Element dvddientichxaydung = doc.createElement("dvddientichxaydung");
                dvddientichxaydung.setTextContent(TinDang.getDientichxaydung().getDvddientichxaydung());
                dientichxaydung.appendChild(dvddientichxaydung);

                Element solieudientichxaydung = doc.createElement("solieu");
                solieudientichxaydung.setTextContent(Double.toString(TinDang.getDientichxaydung().getSolieu()));
                dientichxaydung.appendChild(solieudientichxaydung);

                //Get column chieu ngang khuon vien
                Element chieungangxaydung = doc.createElement("chieungangxaydung");
                tindang.appendChild(chieungangxaydung);

                Element dvdchieungangxaydung = doc.createElement("dvdchieungangxaydung");
                dvdchieungangxaydung.setTextContent(TinDang.getChieungangxaydung().getDvdchieungangxaydung());
                chieungangxaydung.appendChild(dvdchieungangxaydung);

                Element solieuchieungangxaydung = doc.createElement("solieu");
                solieuchieungangxaydung.setTextContent(Double.toString(TinDang.getChieungangxaydung().getSolieu()));
                chieungangxaydung.appendChild(solieuchieungangxaydung);

                //Get column chieu dai khuon vien
                Element chieudaixaydung = doc.createElement("chieudaixaydung");
                tindang.appendChild(chieudaixaydung);

                Element dvdchieudaixaydung = doc.createElement("dvdchieudaixaydung");
                dvdchieudaixaydung.setTextContent(TinDang.getChieudaixaydung().getDvdchieudaixaydung());
                chieudaixaydung.appendChild(dvdchieudaixaydung);

                Element solieuchieudaixaydung = doc.createElement("solieu");
                solieuchieudaixaydung.setTextContent(Double.toString(TinDang.getChieudaikhuonvien().getSolieu()));
                chieudaixaydung.appendChild(solieuchieudaixaydung);
                // END GET-----------------------------------
                Element diachi = doc.createElement("diachi");
                diachi.setTextContent(TinDang.getDiachi());
                tindang.appendChild(diachi);
                //Get khu vuc - quan huyen - tinh thanh
                Element khuvuc = doc.createElement("khuvuc");
                tindang.appendChild(khuvuc);

                Element quanhuyen = doc.createElement("quanhuyen");
                quanhuyen.setTextContent(TinDang.getKhuvuc().getQuanhuyen());
                khuvuc.appendChild(quanhuyen);

                Element tinhthanh = doc.createElement("tinhthanh");
                tinhthanh.setTextContent(TinDang.getKhuvuc().getTinhthanh());
                khuvuc.appendChild(tinhthanh);
                //END GET------------------------------------

                Element giatien = doc.createElement("giatien");
                tindang.appendChild(giatien);

                Element dvdgiatien = doc.createElement("dvdgiatien");
                dvdgiatien.setTextContent(TinDang.getGiatien().getDvdgiatien());
                giatien.appendChild(dvdgiatien);

                Element solieugiatien = doc.createElement("solieu");
                solieugiatien.setTextContent(TinDang.getGiatien().getSolieu() + "");
                giatien.appendChild(solieugiatien);



                //---------------------------------------------



                Element hinhanh = doc.createElement("hinhanh");
                hinhanh.setTextContent(TinDang.getHinhanh());
                tindang.appendChild(hinhanh);

                Element tieudetin = doc.createElement("tieudetin");
                tieudetin.setTextContent(TinDang.getTieudetin());
                tindang.appendChild(tieudetin);

                Element tenloaitin = doc.createElement("tenloaitin");
                tenloaitin.setTextContent(TinDang.getTenloaitin());
                tindang.appendChild(tenloaitin);

                Element nguoidangtin = doc.createElement("nguoidangtin");
                nguoidangtin.setTextContent(TinDang.getNguoidangtin());
                tindang.appendChild(nguoidangtin);

                Element ngaydangtin = doc.createElement("ngaydangtin");
                ngaydangtin.setTextContent(TinDang.getNgaydangtin());
                tindang.appendChild(ngaydangtin);

                Element lancuoicapnhat = doc.createElement("lancuoicapnhat");
                lancuoicapnhat.setTextContent(TinDang.getLancuoicapnhat());
                tindang.appendChild(lancuoicapnhat);

                Element soluotxem = doc.createElement("soluotxem");
                soluotxem.setTextContent(TinDang.getSoluotxem().toString());
                tindang.appendChild(soluotxem);

                Element isActive = doc.createElement("isActive");
                isActive.setTextContent("True");
                tindang.appendChild(isActive);
            }
            //</editor-fold>

            TransformerFactory tsf = TransformerFactory.newInstance();
            Transformer ts = tsf.newTransformer();

            ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            ts.setOutputProperty(OutputKeys.INDENT, "no");

            StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
            DOMSource dom = new DOMSource(doc);
            ts.transform(dom, rs);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void createXMLfromTblTinDang(TblTinDang[] tblTindangs, String outputXML) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
//            doc.setXmlStandalone(false);


            Element dstindang = doc.createElement("dstindang");
            doc.appendChild(dstindang);


            if (tblTindangs != null) {
                for (int i = 0; i < tblTindangs.length; i++) {
                    TblTinDang tblTinDang = tblTindangs[i];
                    Element tindang = doc.createElement("tindang");
                    tindang.setAttribute("status", "normal");
                    dstindang.appendChild(tindang);

                    Element matindang = doc.createElement("matindang");
                    matindang.setTextContent(tblTinDang.getMatindang() + "");
                    tindang.appendChild(matindang);

                    Element tenloainha = doc.createElement("tenloainha");
                    tenloainha.setTextContent(tblTinDang.getTblLoaiNha().getTenloainha());
                    tindang.appendChild(tenloainha);

                    Element tenloaigiayto = doc.createElement("tenloaigiayto");
                    tenloaigiayto.setTextContent(tblTinDang.getTblLoaiGiayTo().getTenloaigiayto());
                    tindang.appendChild(tenloaigiayto);

                    //Get column dientichkhuonvien: 40 - dvddientichkhuongvien : m2
                    Element dientichkhuonvien = doc.createElement("dientichkhuonvien");
                    tindang.appendChild(dientichkhuonvien);

                    Element dvddientichkhuonvien = doc.createElement("dvddientichkhuonvien");
                    dvddientichkhuonvien.setTextContent(tblTinDang.getDvddientichkhuonvien());
                    dientichkhuonvien.appendChild(dvddientichkhuonvien);

                    Element solieu = doc.createElement("solieu");
                    solieu.setTextContent(tblTinDang.getDientichkhuonvien().toString());
                    dientichkhuonvien.appendChild(solieu);
                    // END GET-----------------------------------

                    //Get column dientichxaydung: 40 - dientichxaydung : m2
                    Element dientichxaydung = doc.createElement("dientichxaydung");
                    tindang.appendChild(dientichxaydung);

                    Element dvddientichxaydung = doc.createElement("dvddientichxaydung");
                    dvddientichxaydung.setTextContent(tblTinDang.getDvddientichxaydung());
                    dientichxaydung.appendChild(dvddientichxaydung);

                    Element solieudientichxaydung = doc.createElement("solieu");
                    solieudientichxaydung.setTextContent(tblTinDang.getDientichxaydung().toString());
                    dientichxaydung.appendChild(solieudientichxaydung);
                    // END GET-----------------------------------

                    //Get khu vuc - quan huyen - tinh thanh
                    Element khuvuc = doc.createElement("khuvuc");
                    tindang.appendChild(khuvuc);

                    Element quanhuyen = doc.createElement("quanhuyen");
                    quanhuyen.setTextContent(tblTinDang.getTblKhuVuc().getQuanhuyen());
                    khuvuc.appendChild(quanhuyen);

                    Element tinhthanh = doc.createElement("tinhthanh");
                    tinhthanh.setTextContent(tblTinDang.getTblKhuVuc().getTinhthanh());
                    khuvuc.appendChild(tinhthanh);
                    //END GET------------------------------------

                    //Get column giatien: 300 - dvdgiatien : VND
                    Element giatien = doc.createElement("giatien");
                    tindang.appendChild(giatien);

                    Element dvdgiatien = doc.createElement("dvdgiatien");
                    dvdgiatien.setTextContent(tblTinDang.getDvdgiatien());
                    giatien.appendChild(dvdgiatien);

                    Element solieugiatien = doc.createElement("solieu");
                    solieugiatien.setTextContent(tblTinDang.getGiatien().toString());
                    giatien.appendChild(solieugiatien);
                    // END GET-----------------------------------
//                Element giatien = doc.createElement("giatien");
//                giatien.setTextContent(tblTinDang.getGiatien().toString());
//                tindang.appendChild(giatien);

                    Element hinhanh = doc.createElement("hinhanh");
                    hinhanh.setTextContent(tblTinDang.getHinhanh());
                    tindang.appendChild(hinhanh);

                    Element tieudetin = doc.createElement("tieudetin");
                    tieudetin.setTextContent(tblTinDang.getTieudetin());
                    tindang.appendChild(tieudetin);

                    Element tenloaitin = doc.createElement("tenloaitin");
                    tenloaitin.setTextContent(tblTinDang.getTblLoaiTin().getTenloaitin());
                    tindang.appendChild(tenloaitin);

                    Element nguoidangtin = doc.createElement("nguoidangtin");
                    nguoidangtin.setTextContent(tblTinDang.getTblNguoiDung().getTendangnhap());
                    tindang.appendChild(nguoidangtin);

                    Element ngaydangtin = doc.createElement("ngaydangtin");
                    ngaydangtin.setTextContent(tblTinDang.getNgaydangtin().toString());
                    tindang.appendChild(ngaydangtin);

                    Element lancuoicapnhat = doc.createElement("lancuoicapnhat");
                    lancuoicapnhat.setTextContent(tblTinDang.getLancuoicapnhat().toString());
                    tindang.appendChild(lancuoicapnhat);

                    Element soluotxem = doc.createElement("soluotxem");
                    soluotxem.setTextContent(tblTinDang.getSoluotxem().toString());
                    tindang.appendChild(soluotxem);
                }
            }


            TransformerFactory tsf = TransformerFactory.newInstance();
            Transformer ts = tsf.newTransformer();
            ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            ts.setOutputProperty(OutputKeys.INDENT, "no");

            StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
            DOMSource dom = new DOMSource(doc);
            ts.transform(dom, rs);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void createXMLfromTblKhuvuc(String outputXML) {
        try {
            KhuVucDAO khuvuc = new KhuVucDAO();
            String[] dstinhthanh = khuvuc.getAllTinhThanh();


            if (dstinhthanh != null) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.newDocument();
//                doc.setXmlStandalone(false);

                Element dskhuvuc = doc.createElement("dskhuvuc");
                doc.appendChild(dskhuvuc);

//                Element tinhthanhhientai = doc.createElement("tinhthanhhientai");
//                dskhuvuc.appendChild(tinhthanhhientai);

                // <editor-fold defaultstate="collapsed" desc="tblkhuvuc">
                // Element dstindang
                for (int i = 0; i < dstinhthanh.length; i++) {
                    Element tinhthanh = doc.createElement("tinhthanh");
                    dskhuvuc.appendChild(tinhthanh);

                    Element tentinhthanh = doc.createElement("tentinhthanh");
                    tentinhthanh.setTextContent(dstinhthanh[i]);
                    tinhthanh.appendChild(tentinhthanh);

                    String[] dsquanhuyen = khuvuc.getAllQuanHuyenByTinhThanh(dstinhthanh[i]);
                    for (int j = 0; j < dsquanhuyen.length; j++) {
                        String tenquanhuyen = dsquanhuyen[j];
                        if(tenquanhuyen==null){
                            tenquanhuyen = " ";
                        }
                        Element quanhuyen = doc.createElement("quanhuyen");
                        quanhuyen.setTextContent(tenquanhuyen);
                        tinhthanh.appendChild(quanhuyen);
                    }
                }
                //</editor-fold>

                TransformerFactory tsf = TransformerFactory.newInstance();
                Transformer ts = tsf.newTransformer();
                //ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                ts.setOutputProperty(OutputKeys.INDENT, "no");

                StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
                DOMSource dom = new DOMSource(doc);
                ts.transform(dom, rs);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void createXMLfromTblLoaiTin(String outputXML) {
        try {
            LoaiTinDAO loaitinDAO = new LoaiTinDAO();
            TblLoaiTin[] loaitinArray = loaitinDAO.getAllLoaiTin();

            if (loaitinArray != null) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.newDocument();
//                doc.setXmlStandalone(false);

                Element dsLoaitin = doc.createElement("dsLoaiTin");
                doc.appendChild(dsLoaitin);

                for (int i = 0; i < loaitinArray.length; i++) {
                    Element loaitin = doc.createElement("loaitin");
                    loaitin.setTextContent(loaitinArray[i].getTenloaitin());
                    dsLoaitin.appendChild(loaitin);

                }


                TransformerFactory tsf = TransformerFactory.newInstance();
                Transformer ts = tsf.newTransformer();
                ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                ts.setOutputProperty(OutputKeys.INDENT, "no");

                StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
                DOMSource dom = new DOMSource(doc);
                ts.transform(dom, rs);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void createXMLfromTblLoaiNha(String outputXML) {
        try {
            LoaiNhaDAO loainhaDAO = new LoaiNhaDAO();
            TblLoaiNha[] loainhaArray = loainhaDAO.getAllLoaiNha();

            if (loainhaArray != null) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.newDocument();
//                doc.setXmlStandalone(false);

                Element dsLoainha = doc.createElement("dsLoaiNha");
                doc.appendChild(dsLoainha);

                for (int i = 0; i < loainhaArray.length; i++) {
                    Element loainha = doc.createElement("loainha");
                    loainha.setTextContent(loainhaArray[i].getTenloainha());
                    dsLoainha.appendChild(loainha);

                }


                TransformerFactory tsf = TransformerFactory.newInstance();
                Transformer ts = tsf.newTransformer();
                ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                ts.setOutputProperty(OutputKeys.INDENT, "no");

                StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
                DOMSource dom = new DOMSource(doc);
                ts.transform(dom, rs);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void createXMLfromTblLoaiGiayTo(String outputXML) {
        try {
            LoaiGiayToDAO loaigiaytoDAO = new LoaiGiayToDAO();
            TblLoaiGiayTo[] loaigiaytoArray = loaigiaytoDAO.getAllLoaiGiayTo();

            if (loaigiaytoArray != null) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.newDocument();
//                doc.setXmlStandalone(false);

                Element dsLoaigiayto = doc.createElement("dsLoaiGiayTo");
                doc.appendChild(dsLoaigiayto);

                for (int i = 0; i < loaigiaytoArray.length; i++) {
                    Element loaigiayto = doc.createElement("loaigiayto");
                    loaigiayto.setTextContent(loaigiaytoArray[i].getTenloaigiayto());
                    dsLoaigiayto.appendChild(loaigiayto);

                }


                TransformerFactory tsf = TransformerFactory.newInstance();
                Transformer ts = tsf.newTransformer();
                ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                ts.setOutputProperty(OutputKeys.INDENT, "no");

                StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
                DOMSource dom = new DOMSource(doc);
                ts.transform(dom, rs);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void createBLANK_TinDangFullXML(String outputXML) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
//            doc.setXmlStandalone(false);
            Element dstindangFull = doc.createElement("dstindang");
            dstindangFull.setTextContent("");
            doc.appendChild(dstindangFull);
            TransformerFactory tsf = TransformerFactory.newInstance();
            Transformer ts = tsf.newTransformer();
            ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            ts.setOutputProperty(OutputKeys.INDENT, "no");
            StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
            DOMSource dom = new DOMSource(doc);
            ts.transform(dom, rs);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *  Create TblLoaiNha.XML from TABLE tblLoaiNha
     * @param loainha
     * @param outputXML
     */
    public void createXMLfromTblLoaiNha(TblLoaiNha[] dsloainha, String outputXML) {
        try {
            if (dsloainha != null) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.newDocument();
//                doc.setXmlStandalone(false);

                Element dsLoainha = doc.createElement("dsloainha");
                doc.appendChild(dsLoainha);

                for (int i = 0; i < dsloainha.length; i++) {
                    Element loainha = doc.createElement("loainha");
                    dsLoainha.appendChild(loainha);

                    Element maloainha = doc.createElement("maloainha");
                    maloainha.setTextContent(dsloainha[i].getMaloainha());
                    loainha.appendChild(maloainha);

                    Element tenloainha = doc.createElement("tenloainha");
                    tenloainha.setTextContent(dsloainha[i].getTenloainha());
                    loainha.appendChild(tenloainha);
                }

                TransformerFactory tsf = TransformerFactory.newInstance();
                Transformer ts = tsf.newTransformer();
                ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                ts.setOutputProperty(OutputKeys.INDENT, "no");

                StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
                DOMSource dom = new DOMSource(doc);
                ts.transform(dom, rs);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Create TblLoaiGiayTo.XML from TABLE tblLoaiGiayTo
     * @param dsloaigiayto
     * @param outputXML
     */
    public void createXMLfromTblLoaiGiayTo(TblLoaiGiayTo[] dsloaigiayto, String outputXML) {
        try {
            if (dsloaigiayto != null) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.newDocument();
//                doc.setXmlStandalone(false);

                Element dsLoaigiayto = doc.createElement("dsloaigiayto");
                doc.appendChild(dsLoaigiayto);

                for (int i = 0; i < dsloaigiayto.length; i++) {
                    Element loaigiayto = doc.createElement("loaigiayto");
                    dsLoaigiayto.appendChild(loaigiayto);

                    Element maloaigiayto = doc.createElement("maloaigiayto");
                    maloaigiayto.setTextContent(dsloaigiayto[i].getMaloaigiayto());
                    loaigiayto.appendChild(maloaigiayto);

                    Element tenloaigiayto = doc.createElement("tenloaigiayto");
                    tenloaigiayto.setTextContent(dsloaigiayto[i].getTenloaigiayto());
                    loaigiayto.appendChild(tenloaigiayto);
                }

                TransformerFactory tsf = TransformerFactory.newInstance();
                Transformer ts = tsf.newTransformer();
                ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                ts.setOutputProperty(OutputKeys.INDENT, "no");

                StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
                DOMSource dom = new DOMSource(doc);
                ts.transform(dom, rs);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Create TblLoaiTin.XML from table tblLoaitin
     * @param dsloaitin
     * @param outputXML
     */
    public void createXMLfromTblLoaiTin(TblLoaiTin[] dsloaitin, String outputXML) {
        try {
            if (dsloaitin != null) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.newDocument();
//                doc.setXmlStandalone(false);

                Element dsLoaitin = doc.createElement("dsloaitin");
                doc.appendChild(dsLoaitin);

                for (int i = 0; i < dsloaitin.length; i++) {
                    Element loaitin = doc.createElement("loaitin");
                    dsLoaitin.appendChild(loaitin);

                    Element maloaitin = doc.createElement("maloaitin");
                    maloaitin.setTextContent(dsloaitin[i].getMaloaitin());
                    loaitin.appendChild(maloaitin);

                    Element tenloaitin = doc.createElement("tenloaitin");
                    tenloaitin.setTextContent(dsloaitin[i].getTenloaitin());
                    loaitin.appendChild(tenloaitin);

                }

                TransformerFactory tsf = TransformerFactory.newInstance();
                Transformer ts = tsf.newTransformer();
                ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                ts.setOutputProperty(OutputKeys.INDENT, "no");

                StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
                DOMSource dom = new DOMSource(doc);
                ts.transform(dom, rs);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Create TblKhuvuc.XML from table tblKhuvuc
     * @param dskhuvucs
     * @param outputXML
     */
    public void createXMLfromTblKhuVuc(TblKhuVuc[] dskhuvucs, String outputXML) {
        try {
            if (dskhuvucs != null) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.newDocument();
             //   doc.setXmlStandalone(false);

                Element dskhuvuc = doc.createElement("dskhuvuc");
                doc.appendChild(dskhuvuc);

                for (int i = 0; i < dskhuvucs.length; i++) {
                    Element khuvuc = doc.createElement("khuvuc");
                    dskhuvuc.appendChild(khuvuc);

                    Element makhuvuc = doc.createElement("makhuvuc");
                    makhuvuc.setTextContent(dskhuvucs[i].getMakhuvuc());
                    khuvuc.appendChild(makhuvuc);

                    Element tinhthanh = doc.createElement("tinhthanh");
                    tinhthanh.setTextContent(dskhuvucs[i].getTinhthanh());
                    khuvuc.appendChild(tinhthanh);

                    Element quanhuyen = doc.createElement("quanhuyen");
                    String quan = dskhuvucs[i].getQuanhuyen();
                    if (quan == null) {
                        quan = " ";
                    }
                    quanhuyen.setTextContent(quan);
                    khuvuc.appendChild(quanhuyen);
                }

                TransformerFactory tsf = TransformerFactory.newInstance();
                Transformer ts = tsf.newTransformer();
                ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                ts.setOutputProperty(OutputKeys.INDENT, "no");

                StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
                DOMSource dom = new DOMSource(doc);
                ts.transform(dom, rs);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void createPDFXMLFullfromTblTinDang(TblTinDang tblTindang, String outputXML) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
//            doc.setXmlStandalone(false);

            Element dstindang = doc.createElement("dstindang");
            doc.appendChild(dstindang);

            Element tindang = doc.createElement("tindang");
            tindang.setAttribute("status", "normal");
            dstindang.appendChild(tindang);

            Element matindang = doc.createElement("matindang");
            matindang.setTextContent(tblTindang.getMatindang() + "");
            tindang.appendChild(matindang);

            Element tenloainha = doc.createElement("tenloainha");
            tenloainha.setTextContent(tblTindang.getTblLoaiNha().getTenloainha());
            tindang.appendChild(tenloainha);

            Element tenloaigiayto = doc.createElement("tenloaigiayto");
            tenloaigiayto.setTextContent(tblTindang.getTblLoaiGiayTo().getTenloaigiayto());
            tindang.appendChild(tenloaigiayto);

            Element cautruc = doc.createElement("cautruc");
            cautruc.setTextContent(tblTindang.getCautruc());
            tindang.appendChild(cautruc);

            Element thongtinchitiet = doc.createElement("thongtinchitiet");
            thongtinchitiet.setTextContent(tblTindang.getThongtinchitiet());
            tindang.appendChild(thongtinchitiet);


            //Get column dientichkhuonvien: 40 - dvddientichkhuongvien : m2
            Element dientichkhuonvien = doc.createElement("dientichkhuonvien");
            tindang.appendChild(dientichkhuonvien);

            Element dvddientichkhuonvien = doc.createElement("dvddientichkhuonvien");
            dvddientichkhuonvien.setTextContent(tblTindang.getDvddientichkhuonvien());
            dientichkhuonvien.appendChild(dvddientichkhuonvien);

            Element solieu = doc.createElement("solieu");
            solieu.setTextContent(tblTindang.getDientichkhuonvien().toString());
            dientichkhuonvien.appendChild(solieu);
            // END GET-----------------------------------
            //Get column chieu ngang khuon vien
            Element chieungangkhuonvien = doc.createElement("chieungangkhuonvien");
            tindang.appendChild(chieungangkhuonvien);

            Element dvdchieungangkhuonvien = doc.createElement("dvdchieungangkhuonvien");
            dvdchieungangkhuonvien.setTextContent(tblTindang.getDvdchieungangkhuonvien());
            chieungangkhuonvien.appendChild(dvdchieungangkhuonvien);

            Element solieuchieungangkhuonvien = doc.createElement("solieu");
            solieuchieungangkhuonvien.setTextContent(Double.toString(tblTindang.getChieungangkhuonvien()));
            chieungangkhuonvien.appendChild(solieuchieungangkhuonvien);

            //Get column chieu dai khuon vien
            Element chieudaikhuonvien = doc.createElement("chieudaikhuonvien");
            tindang.appendChild(chieudaikhuonvien);

            Element dvdchieudaikhuonvien = doc.createElement("dvdchieudaikhuonvien");
            dvdchieudaikhuonvien.setTextContent(tblTindang.getDvdchieudaikhuonvien());
            chieudaikhuonvien.appendChild(dvdchieudaikhuonvien);

            Element solieuchieudaikhuonvien = doc.createElement("solieu");
            solieuchieudaikhuonvien.setTextContent(Double.toString(tblTindang.getChieudaikhuonvien()));
            chieudaikhuonvien.appendChild(solieuchieudaikhuonvien);
            // END GET-----------------------------------


            //Get column dientichxaydung: 40 - dientichxaydung : m2
            Element dientichxaydung = doc.createElement("dientichxaydung");
            tindang.appendChild(dientichxaydung);

            Element dvddientichxaydung = doc.createElement("dvddientichxaydung");
            dvddientichxaydung.setTextContent(tblTindang.getDvddientichxaydung());
            dientichxaydung.appendChild(dvddientichxaydung);

            Element solieudientichxaydung = doc.createElement("solieu");
            solieudientichxaydung.setTextContent(tblTindang.getDientichxaydung().toString());
            dientichxaydung.appendChild(solieudientichxaydung);
            // END GET-----------------------------------

            //Get column chieu ngang khuon vien
            Element chieungangxaydung = doc.createElement("chieungangxaydung");
            tindang.appendChild(chieungangxaydung);

            Element dvdchieungangxaydung = doc.createElement("dvdchieungangxaydung");
            dvdchieungangxaydung.setTextContent(tblTindang.getDvdchieungangxaydung());
            chieungangxaydung.appendChild(dvdchieungangxaydung);

            Element solieuchieungangxaydung = doc.createElement("solieu");
            solieuchieungangxaydung.setTextContent(Double.toString(tblTindang.getChieungangxaydung()));
            chieungangxaydung.appendChild(solieuchieungangxaydung);

            //Get column chieu dai khuon vien
            Element chieudaixaydung = doc.createElement("chieudaixaydung");
            tindang.appendChild(chieudaixaydung);

            Element dvdchieudaixaydung = doc.createElement("dvdchieudaixaydung");
            dvdchieudaixaydung.setTextContent(tblTindang.getDvdchieudaixaydung());
            chieudaixaydung.appendChild(dvdchieudaixaydung);

            Element solieuchieudaixaydung = doc.createElement("solieu");
            solieuchieudaixaydung.setTextContent(Double.toString(tblTindang.getChieudaikhuonvien()));
            chieudaixaydung.appendChild(solieuchieudaixaydung);
            // END GET-----------------------------------
            Element diachi = doc.createElement("diachi");
            diachi.setTextContent(tblTindang.getDiachi());
            tindang.appendChild(diachi);

            //Get khu vuc - quan huyen - tinh thanh
            Element khuvuc = doc.createElement("khuvuc");
            tindang.appendChild(khuvuc);

            Element quanhuyen = doc.createElement("quanhuyen");
            quanhuyen.setTextContent(tblTindang.getTblKhuVuc().getQuanhuyen());
            khuvuc.appendChild(quanhuyen);

            Element tinhthanh = doc.createElement("tinhthanh");
            tinhthanh.setTextContent(tblTindang.getTblKhuVuc().getTinhthanh());
            khuvuc.appendChild(tinhthanh);
            //END GET------------------------------------

            //Get column giatien: 300 - dvdgiatien : VND
            Element giatien = doc.createElement("giatien");
            tindang.appendChild(giatien);

            Element dvdgiatien = doc.createElement("dvdgiatien");
            dvdgiatien.setTextContent(tblTindang.getDvdgiatien());
            giatien.appendChild(dvdgiatien);

            Element solieugiatien = doc.createElement("solieu");
            solieugiatien.setTextContent(tblTindang.getGiatien().toString());
            giatien.appendChild(solieugiatien);
            // END GET-----------------------------------
//                Element giatien = doc.createElement("giatien");
//                giatien.setTextContent(tblTinDang.getGiatien().toString());
//                tindang.appendChild(giatien);

            Element hinhanh = doc.createElement("hinhanh");
            hinhanh.setTextContent(tblTindang.getHinhanh());
            tindang.appendChild(hinhanh);

            Element tieudetin = doc.createElement("tieudetin");
            tieudetin.setTextContent(tblTindang.getTieudetin());
            tindang.appendChild(tieudetin);

            Element tenloaitin = doc.createElement("tenloaitin");
            tenloaitin.setTextContent(tblTindang.getTblLoaiTin().getTenloaitin());
            tindang.appendChild(tenloaitin);

            Element nguoidangtin = doc.createElement("nguoidangtin");
            nguoidangtin.setTextContent(tblTindang.getTblNguoiDung().getTendangnhap());
            tindang.appendChild(nguoidangtin);

            Element tennguoidangtin = doc.createElement("tennguoidangtin");
            tennguoidangtin.setTextContent(tblTindang.getTblNguoiDung().getHo() + " " + tblTindang.getTblNguoiDung().getTen());
            tindang.appendChild(tennguoidangtin);

            Element dienthoai = doc.createElement("dienthoai");
            dienthoai.setTextContent(tblTindang.getTblNguoiDung().getDienthoai());
            tindang.appendChild(dienthoai);

            Element dienthoaididong = doc.createElement("dienthoaididong");
            dienthoaididong.setTextContent(tblTindang.getTblNguoiDung().getDienthoaididong());
            tindang.appendChild(dienthoaididong);

            Element email = doc.createElement("email");
            email.setTextContent(tblTindang.getTblNguoiDung().getEmail());
            tindang.appendChild(email);

            Element ngaydangtin = doc.createElement("ngaydangtin");
            ngaydangtin.setTextContent(tblTindang.getNgaydangtin().toString());
            tindang.appendChild(ngaydangtin);

            Element lancuoicapnhat = doc.createElement("lancuoicapnhat");
            lancuoicapnhat.setTextContent(tblTindang.getLancuoicapnhat().toString());
            tindang.appendChild(lancuoicapnhat);

            Element soluotxem = doc.createElement("soluotxem");
            soluotxem.setTextContent(tblTindang.getSoluotxem().toString());
            tindang.appendChild(soluotxem);

            Element isActive = doc.createElement("isActive");
            isActive.setTextContent(tblTindang.getIsActived() + "");
            tindang.appendChild(isActive);



            TransformerFactory tsf = TransformerFactory.newInstance();
            Transformer ts = tsf.newTransformer();
            ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            ts.setOutputProperty(OutputKeys.INDENT, "no");

            StreamResult rs = new StreamResult(new FileOutputStream(outputXML));
            DOMSource dom = new DOMSource(doc);
            ts.transform(dom, rs);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
